﻿@model SuperDumpService.ViewModels.DumpsViewModel
@{
	ViewData["Title"] = "Dumps";
}
<h2>All dumps</h2>
<div class="container">
	<div class="panel panel-default">
		@{
			bool isSimpleFilterSet = !string.IsNullOrEmpty(ViewData["searchFilter"] as string);
			bool isElasticFilterSet = !string.IsNullOrEmpty(ViewData["elasticSearchFilter"] as string);

			var simpleSearchClasses = (isSimpleFilterSet || !isElasticFilterSet) ? "active show" : string.Empty;
			var elasticSearchClasses = isElasticFilterSet ? "active show" : string.Empty;
		}
		<div class="panel-heading">
			<ul class="nav nav-tabs">
				<li class="nav-item"><a class="nav-link @simpleSearchClasses" href="#simplesearch" data-toggle="tab">Simple Search</a></li>
				<li class="nav-item"><a class="nav-link @elasticSearchClasses" href="#elasticsearch" data-toggle="tab">Elasticsearch</a></li>
			</ul>
		</div>
		<div class="panel-body">
			<div class="tab-content">
				<div class="tab-pane fade @simpleSearchClasses" id="simplesearch">
					<form asp-action="Dumps" method="get">
						<div class="form-actions no-color">
							<p>
								<input type="text" name="searchFilter" class="form-control float-left w-75 p-3" value="@ViewData["searchFilter"]" />
								<input type="hidden" name="elasticSearch" value="false" />
								<input type="submit" value="Search" class="btn btn-primary" />
								@if (!string.IsNullOrEmpty(ViewData["searchFilter"] as string)) {
									<a asp-action="Dumps">Clear search</a>
								}
							</p>
						</div>
					</form>
				</div>
				<div class="tab-pane fade @elasticSearchClasses" id="elasticsearch">
					<form asp-action="Dumps" method="get">
						<div class="form-group">
							<label for="searchFilter">Elasticsearch query:</label>
							<textarea class="form-control courier-small" rows="10" id="elasticSearchFilter" name="elasticSearchFilter">
								@if (string.IsNullOrEmpty(ViewData["elasticSearchFilter"] as string)) {
<text>{
    "query": {
        "wildcard": {
            "loadedModules": "clr*"
        }
    },
    "size": "1000"
}
</text>
								} else {
									@ViewData["elasticSearchFilter"]
								}
							</textarea>
						</div>
						<div class="alert alert-light" role="alert">
							You can run an arbitrary <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/_introducing_the_query_language.html">Elastic Search query language</a> here. To see which fields are enabled, take a look via <a href="@Model.KibanaUrl/app/kibana#/discover?_g=(time:(from:now-1y,mode:quick,to:now))">Kibana</a>.
						</div>
						<div class="form-actions no-color">
							<input type="submit" value="Search" class="btn btn-primary" />
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
	@if (!Model.IsPopulated) {
		<div>
			<span class="glyphicon glyphicon-exclamation-sign"></span> <strong>Bundles/Dumps</strong> are being populated right now. List is not complete yet.
		</div>
	}
	@if (!Model.IsRelationshipsPopulated) {
		<div>
			<span class="glyphicon glyphicon-exclamation-sign"></span> <strong>Similarity repository</strong> is being populated right now. Similarities might not be complete yet.
		</div>
	}
	@if (!Model.IsJiraIssuesPopulated) {
		<div>
			<span class="glyphicon glyphicon-exclamation-sign"></span> <strong>Jira issue repository</strong> is populated right now. Linked jira issues might not be complete yet.
		</div>
	}
	<p>Showing @Model.Filtered.Count() dumps.
		@if (ViewData["duplBundleId"] != null) {
			<span>(duplicates of <a href="@SuperDumpService.Helpers.Utility.GetDumpUrl(SuperDumpService.Models.DumpIdentifier.Create(ViewData["duplBundleId"].ToString(), ViewData["duplDumpId"].ToString()))">@ViewData["duplBundleId"]:@ViewData["duplDumpId"]</a>)</span>
		}
	</p>
	<div class="panel-body">
		<div id="cal-heatmap"></div>
		<script type="text/javascript">

			calHeatMap3weeks();

			function calHeatMap3weeks() {
				$("cal-heatmap").innerHTML = "";
				let cal = new CalHeatMap();
				cal.init({
					start: new Date().setDate((new Date()).getDate() - 21),
					range: 22,
					domain: "day",
					//subDomain: "day",
					data: window.location.protocol + "//" + window.location.host
						+ "/api/Dumps/Heatmap?start={{d:start}}&stop={{d:end}}&searchFilter=@System.Web.HttpUtility.UrlEncode((string)ViewData["searchFilter"])"
						+ "&elasticSearchFilter=@Html.Raw(System.Web.HttpUtility.UrlEncode((string)ViewData["elasticSearchFilter"]))"
						+ "&duplBundleId=@ViewData["duplBundleId"]&duplDumpId=@ViewData["duplDumpId"]"
				});
			}
		</script>
	</div>
	@{Html.RenderPartial("_DumpList", Model);}
</div>
